package com.gen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gen.entity.Student;
import com.gen.service.IStudentService;
import com.sun.java.swing.plaf.motif.resources.motif_de;

@Controller
public class StudentController {
	@Autowired
	IStudentService studentService;

	@RequestMapping("/addPage.do")
	public String addPage() {
		return "add";
	}
	
	@RequestMapping("/add.do")
	public ModelAndView add(Student student) {
		ModelAndView modelAndView = new ModelAndView();
		studentService.add(student);
		modelAndView.setViewName("redirect:selectAll.do");
		return modelAndView;
	}
	
	@RequestMapping("/delete.do")
	public ModelAndView delete(Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		studentService.delete(id);
		modelAndView.setViewName("redirect:selectAll.do");
		return modelAndView;
	}
	
	@RequestMapping("/update.do")
	public ModelAndView update(Student student) {
		ModelAndView modelAndView = new ModelAndView();
		studentService.update(student);
		modelAndView.setViewName("redirect:selectAll.do");
		return modelAndView;
	}
	
	@RequestMapping("/selectAll.do")
	public ModelAndView selectAll() {
		ModelAndView modelAndView = new ModelAndView();
		List<Student> all = studentService.selectAll();
		modelAndView.addObject("all", all);
		modelAndView.setViewName("list");
		return modelAndView;
	}
	
	@RequestMapping("/selectOne.do")
	public ModelAndView selectOne(Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Student student = studentService.selectOne(id);
		modelAndView.addObject("student", student);
		modelAndView.setViewName("detail");
		return modelAndView;
	}
	
	@RequestMapping("/toUpdate.do")
	public ModelAndView toUpdate(Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Student student = studentService.selectOne(id);
		modelAndView.addObject("student", student);
		modelAndView.setViewName("update");
		return modelAndView;
	}
	
	@RequestMapping("/selectByParam.do")
	public ModelAndView selectByParam() {
		ModelAndView modelAndView = new ModelAndView();
		Student student = new Student();
		student.setSex('女');
		student.setAddress("北");
		List<Student> all = studentService.selectByParam(student);
		modelAndView.addObject("all", all);
		modelAndView.setViewName("list");
		return modelAndView;
	}
}
